package com.cuit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.UserMapper;
import com.cuit.pojo.BookUser;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Api(tags="关注")
@RestController
//@RequestMapping("/subscribe")
public class SubscribeController {
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

//    private Jedis jedis = new Jedis("47.109.38.52");

    @GetMapping("/subscribeNum")
    public R getSubscribeNum(String username){
        Jedis jedis = new Jedis("47.109.38.52");
        jedis.auth("123456");
        long num = 0;
        if (jedis.exists(username)){
            num = jedis.llen(username);
        }
        System.out.println("subscribeNumer: "+num);
        jedis.close();
        return R.ok().data("subscribeNum",num);
    }

    @GetMapping("/fansNum")
    public R getFansNum(String username){
        Jedis jedis = new Jedis("47.109.38.52");
        jedis.auth("123456");
        long num = 0;
        if (jedis.exists(username+"fans")){
            num = jedis.llen(username+"fans");
        }
        System.out.println("FansNumer: "+num);
        jedis.close();
        return R.ok().data("fansNum",num);
    }

    @GetMapping("/subscribeList")
    public R subscribe(String username){
        Jedis jedis = new Jedis("47.109.38.52");
        jedis.auth("123456");
        List<User> userList = new ArrayList<>();
        List<User> returnList = new ArrayList<>();
        if (jedis.exists(username)){
            Long length = jedis.llen(username);

            List<String> list  = jedis.lrange(username,0,length-1);
//            System.out.println(username);
//            System.out.println(list);

            for (int i = 0;i < length;i++){
                User user = new User();
                QueryWrapper<User> queryWrapper= new QueryWrapper();
                queryWrapper.like("username",list.get(i));
                System.out.println(userList);
                userList = userMapper.selectList(queryWrapper);
                user.setAvatar("http://47.109.38.52:80/"+userList.get(0).getAvatar());
                user.setUsername(userList.get(0).getUsername());
                returnList.add(user);
            }
//            userList = userService.listByIds(list);
            System.out.println(returnList);
        }
        jedis.close();
        return R.ok().data("subscribeList",returnList);
    }
    @GetMapping("/fansList")
    public R fansList(String username){
        Jedis jedis = new Jedis("47.109.38.52");
        jedis.auth("123456");
        List<User> userList = new ArrayList<>();
        List<User> returnList = new ArrayList<>();
        String fans = username+"fans";
        if (jedis.exists(fans)){
            Long length = jedis.llen(fans);
            List<String> list  = jedis.lrange(fans,0,length-1);
            System.out.println(list);

            for (int i = 0;i < length;i++){
                User user = new User();
                QueryWrapper<User> queryWrapper= new QueryWrapper();
                queryWrapper.like("username",list.get(i));
                userList = userMapper.selectList(queryWrapper);
                user.setAvatar("http://47.109.38.52:80/"+userList.get(0).getAvatar());
                user.setUsername(userList.get(0).getUsername());
                returnList.add(user);
            }
        }
        jedis.close();
        return R.ok().data("fansList",returnList);
    }

}
